﻿
using RTW.DAL;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Servers
{
    public class UserServers
    {
        private readonly IRepository<user> userRepository;
        private readonly IRepository<department> departmentRepository;
        //private readonly IRepository<role> roleRepository;

        public UserServers()
        {
            userRepository = new DapperRepository<user>();
            departmentRepository = new DapperRepository<department>();
            //roleRepository = new DapperRepository<role>();
        }

        public List<user> GetData()
        {
            return userRepository.GetAllEnumerable().ToList();
        }

        public user GetModelById(string id)
        {
            return userRepository.GetById(id);
        }

        public string GetUserNO(string userId)
        {
            string sql = string.Format("SELECT [USER_NO] FROM USERS WHERE [USER_ID]='{0}'", userId);
            var result = userRepository.SqlWithParamsDynamic(sql);
            foreach (var item in result)
            {
                return item.USER_NO;
            }
            return null;
        }

        internal void Add(user model)
        {
            userRepository.Insert(model);
        }

        internal void Update(user model)
        {
            userRepository.Update(model);
        }

        internal void Del(string id)
        {
            userRepository.DeleteByIds(id);
        }


        internal List<SelectListItem> GetDEPT_ID()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = departmentRepository.SqlWithParamsDynamic("select DEPT_ID,DEPT_NAME,LEVEL from department");
            tables.Clear();
            foreach (var dr in dstable)
            {
                string Id = dr.DEPT_ID.ToString();
                int ClassLayer = int.Parse(dr.LEVEL.ToString());
                string Title = dr.DEPT_NAME.ToString().Trim();

                if (ClassLayer == 0)
                {
                    tables.Add(new SelectListItem { Value = Id, Text = Title });
                }
                else
                {
                    Title = "├ " + Title;
                    Title = StringUtil.StringOfChar(ClassLayer, "　") + Title;
                    tables.Add(new SelectListItem { Value = Id, Text = Title });
                }
            }
            //foreach (var dr in dstable)
            //{

            //    string Id = dr.DEPT_ID.ToString();
            //    string Title = dr.DEPT_NAME.ToString().Trim();
            //    tables.Add(new SelectListItem { Value = Id, Text = Title });

            //}

            return tables;
        }

        internal IList<user> GetDataByDRPT_ID(int dept_id)
        {
            IList<user> tables = new List<user>();
            var dstable = departmentRepository.SqlWithParamsDynamic("SELECT [USER_NAME],[TRUE_NAME],[USER_NO],[DEPT_ID],[USER_ID] FROM [OADB].[dbo].[users] WHERE [DEPT_ID]=" + dept_id);
            tables.Clear();

            foreach (var dr in dstable)
            {
                tables.Add(new user
                {
                    USER_NAME = dr.USER_NAME,
                    TRUE_NAME = dr.TRUE_NAME,
                    USER_NO = dr.USER_NO,
                    DEPT_ID = dr.DEPT_ID,
                    USER_ID = dr.USER_ID
                });
            }

            return tables;
        }

        internal List<SelectListItem> GetPOST_PRIV()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = userRepository.SqlWithParamsDynamic("select CODE_NO Value,concat(CODE_NO,'-----',CODE_NAME) Text  from [dbo].[sys_code] where PARENT_NO=167");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }


        internal List<SelectListItem> GetLEAVE_DEPT()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = userRepository.SqlWithParamsDynamic("select DEPT_ID,DEPT_NAME from department ");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.DEPT_ID.ToString();
                string Title = dr.DEPT_NAME.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }

        internal List<SelectListItem> GetUSER_PRIV()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = userRepository.SqlWithParamsDynamic("select concat(ROLE_NO,'-----',ROLE_NAME) Text, ROLE_ID Value from role ");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }

        internal user GetLeaderByUserNO(string userNO)
        {
            string sql = "SELECT U.* FROM [OADB].[dbo].[users] AS U INNER JOIN [OADB].[dbo].[department] AS D ON U.DEPT_ID = D.DEPT_ID WHERE U.USER_NO = D.LEADER1 AND U.USER_NO = '" + userNO + "'";
            return userRepository.SqlWithParamsSingle(sql);
        }

    }
}